<?php

// Namespace

namespace Loop;


/**
 * CArrayHelper is a helper that contains various functions to help developers deal with arrays
 *
 * @package		Loop
 * @category	Helper
 */

class CArrayHelper
{
	/**
	 * Get the depth of the array
	 * 
	 * @param	array	$array 	The array
	 *
	 * @return 	int 	The number of depth of the array
	 */

	public function getDepth($array)
	{
		$maxDepth = 1;

		foreach ($array as $value)
		{
			if
			(
				(is_array($value)) ||
				(is_object($value))
			)
			{
				$depth = $this->getDepth($value) + 1;

				if ($depth > $maxDepth)
				{
					$maxDepth = $depth;
				}
			}
		}


		return $maxDepth;
	}


	/**
	 * Order an associative array by key
	 *
	 * @param  array 	$arrayToSort 		The array to sort
	 * @param  string 	$orderBy 			The key for order
	 * @param  string 	$orderDirection 	Asc or desc
	 *
	 * @return array The array to sort
	 */

	public function orderByKey($arrayToSort, $orderBy, $orderDirection = SORT_ASC, $preserveKeys = false)
	{
		// Create variables

		$arrayResult = array();
		$arraySortable = array();


		// Is there anything to sort?

		if (count($arrayToSort) > 0)
		{
			foreach ($arrayToSort as $entry => $entryValue)
			{
				// Is the value an array ?

				if (is_array($entryValue))
				{
					// For each entry of the value

					foreach ($entryValue as $key => $value)
					{
						// Sort

						if ($key == $orderBy)
						{
							$arraySortable[$entry] = $value;
						}
					}
				}
				else
				{
					// Not an array

					$arraySortable[$entry] = $entryValue;
				}
			}


			// Which order ?

			switch ($orderDirection)
			{
				case SORT_ASC :
							asort($arraySortable);
						break;

				case SORT_DESC :
							arsort($arraySortable);
						break;
			}

			//

			if ($preserveKeys == false)
			{
				// Create array result

				$cptKey = 0;

				foreach ($arraySortable as $key => $value)
				{
					$arrayResult[$cptKey] = $arrayToSort[$key];

					$cptKey ++;
				}
			}
			else
			{
				foreach ($arraySortable as $key => $value)
				{
					$arrayResult[$key] = $arrayToSort[$key];
				}	
			}
		}

		return $arrayResult;
	}


	/**
	 * Slice an associative array, It slices by array key from and including that key.
	 *
	 * @param array 		$arrayToSlice 	The array to slice
	 * @param string 		$key 			The key
	 * @param string/int 	$lenght 		The length or the key to slice up
	 * @param boolean 		$preserveKeys  	Do we preserve the key ?
	 * 
	 * @return array 		The array
	 */


	public function sliceAssociativeArray($arrayToSlice, $key, $length, $preserveKeys = true)
	{
		// Get the offset

		$offset = array_search($key, array_keys($arrayToSlice));


		// Is the lenght a string or an int

		if (is_string($length) === true)
		{
			// $length is a string it is assumed to be another array key and the array is sliced up to but not including the end key
			// otherwise it slices that length.
		
			$length = array_search($length, array_keys($arrayToSlice)) - $offset;
		}

		
		$result = array_slice($arrayToSlice, $offset, $length, $preserveKeys);


		return $result;
	}


	/**
	 * Check if the array has no value 
	 *
	 * @param 	array 	$array 		The array associative array
	 *
	 * @return 	boolean  return true if the associative array has no values, false if any
	 */

	public function checkAssociativeArrayIsEmpty($array)
	{
		foreach ($array as $key => $value)
		{
			if (empty($value) === false)
			{
				return false;
			}
		}

		return true;
	}
}

?>
